﻿using YDT_ProductService.Domains.Enums;

namespace YDT_ProductService.Domains.Entities
{
    /// <summary>
    /// 商品聚合根 - 商品是聚合根，包含商品图片集合
    /// 聚合根：负责维护自身和子实体的业务规则完整性
    /// </summary>
    public class Product : Entity, IAggregateRoot
    {
        /// <summary>商品编码 - 业务唯一标识</summary>
        public string ProductCode { get; private set; }
        /// <summary>商品主图URL</summary>
        public string ProductUrl { get; private set; }
        /// <summary>商品标题</summary>
        public string ProductTitle { get; private set; }
        /// <summary>商品图文描述</summary>
        public string ProductDescription { get; private set; }
        /// <summary>商品虚拟价格（原价/划线价）</summary>
        public decimal ProductVirtualprice { get; private set; }
        /// <summary>商品实际价格</summary>
        public decimal ProductPrice { get; private set; }
        /// <summary>商品排序序号</summary>
        public int ProductSort { get; private set; }
        /// <summary>商品已售数量</summary>
        public int ProductSold { get; private set; }
        /// <summary>商品库存数量</summary>
        public int ProductStock { get; private set; }
        /// <summary>商品状态</summary>
        public ProductStatus ProductStatus { get; private set; }
        /// <summary>创建时间</summary>
        public DateTime CreateTime { get; private set; }
        /// <summary>更新时间</summary>
        public DateTime UpdateTime { get; set; }

        /// <summary>商品图片集合 - 私有字段确保通过方法修改</summary>
        private readonly List<ProductImage> _productImages = new();
        /// <summary>商品图片只读集合</summary>
        public IReadOnlyCollection<ProductImage> ProductImages => _productImages.AsReadOnly();

        /// <summary>
        /// 受保护的构造函数 - 用于EF Core
        /// </summary>
        protected Product() { }

        /// <summary>
        /// 创建商品构造函数
        /// </summary>
        public Product(Guid Id,string productCode, string productTitle, string productDescription,
                      decimal productPrice, int productStock, string productUrl = "",
                      decimal productVirtualprice = 0, int productSort = 0)
        {
            Id = Id;
            ProductCode = productCode;
            ProductTitle = productTitle;
            ProductDescription = productDescription;
            ProductPrice = productPrice;
            ProductStock = productStock;
            ProductUrl = productUrl;
            ProductVirtualprice = productVirtualprice;
            ProductSort = productSort;
            ProductSold = 0;
            ProductStatus = ProductStatus.Active; // 默认激活状态
            CreateTime = DateTime.UtcNow;
            UpdateTime = DateTime.UtcNow;

            // 验证业务规则
            ValidateCreation();
        }

        /// <summary>
        /// 更新商品基本信息
        /// </summary>
        public void UpdateBasicInfo(string productTitle, string productDescription,
                                   decimal productPrice, decimal productVirtualprice,
                                   int productSort, string productUrl = "")
        {
            ProductTitle = productTitle;
            ProductDescription = productDescription;
            ProductPrice = productPrice;
            ProductVirtualprice = productVirtualprice;
            ProductSort = productSort;

            if (!string.IsNullOrEmpty(productUrl))
            {
                ProductUrl = productUrl;
            }

            UpdateTime = DateTime.UtcNow;

            ValidateBasicInfo();
        }

        /// <summary>
        /// 更新库存
        /// </summary>
        public void UpdateStock(int newStock)
        {
            if (newStock < 0)
                throw new ArgumentException("库存不能为负数");

            ProductStock = newStock;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 增加库存
        /// </summary>
        public void IncreaseStock(int quantity)
        {
            if (quantity <= 0)
                throw new ArgumentException("增加数量必须大于0");

            ProductStock += quantity;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 减少库存
        /// </summary>
        public void DecreaseStock(int quantity)
        {
            if (quantity <= 0)
                throw new ArgumentException("减少数量必须大于0");

            if (ProductStock < quantity)
                throw new InvalidOperationException("库存不足");

            ProductStock -= quantity;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 增加销量
        /// </summary>
        public void IncreaseSold(int quantity)
        {
            if (quantity <= 0)
                throw new ArgumentException("销售数量必须大于0");

            ProductSold += quantity;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 激活商品
        /// </summary>
        public void Activate()
        {
            ProductStatus = ProductStatus.Active;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 禁用商品
        /// </summary>
        public void Deactivate()
        {
            ProductStatus = ProductStatus.Inactive;
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 添加商品图片
        /// </summary>
        public void AddProductImage(string imageUrl, int imageSort = 0, string imageStatus = "1")
        {
            var productImage = new ProductImage(Id, imageUrl, imageSort, imageStatus);
            _productImages.Add(productImage);
            UpdateTime = DateTime.UtcNow;
        }

        /// <summary>
        /// 移除商品图片
        /// </summary>
        public void RemoveProductImage(Guid imageId)
        {
            var image = _productImages.FirstOrDefault(x => x.Id == imageId);
            if (image != null)
            {
                _productImages.Remove(image);
                UpdateTime = DateTime.UtcNow;
            }
        }

        /// <summary>
        /// 更新商品图片排序
        /// </summary>
        public void UpdateProductImageSort(Guid imageId, int newSort)
        {
            var image = _productImages.FirstOrDefault(x => x.Id == imageId);
            if (image != null)
            {
                image.UpdateSort(newSort);
                UpdateTime = DateTime.UtcNow;
            }
        }

        /// <summary>
        /// 验证商品创建时的业务规则
        /// </summary>
        private void ValidateCreation()
        {
            if (string.IsNullOrWhiteSpace(ProductCode))
                throw new ArgumentException("商品编码不能为空");

            if (string.IsNullOrWhiteSpace(ProductTitle))
                throw new ArgumentException("商品标题不能为空");

            if (ProductPrice < 0)
                throw new ArgumentException("商品价格不能为负数");

            if (ProductStock < 0)
                throw new ArgumentException("商品库存不能为负数");
        }

        /// <summary>
        /// 验证基本信息
        /// </summary>
        private void ValidateBasicInfo()
        {
            if (string.IsNullOrWhiteSpace(ProductTitle))
                throw new ArgumentException("商品标题不能为空");

            if (ProductPrice < 0)
                throw new ArgumentException("商品价格不能为负数");

            if (ProductVirtualprice < 0)
                throw new ArgumentException("商品虚拟价格不能为负数");
        }
    }
}
